﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;

namespace WGLGridView
{
	public class WGLGridView : GridView
	{
		//当Gridview数据为空时显示的信息
		private static string EmptyText = "没有记录";

		public WGLGridView()
		{
		}
		/// <summary>
		/// 防止PostBack后Gridview不能显示
		/// </summary>
		/// <param name="gridview"></param>
		public static void ClearAndShowEmptyGridView(GridView gridview, string[] fieldStrings)
		{
			//如果数据为空则重新构造Gridview
			DataTable dt = new DataTable();
			int nCount = fieldStrings.Length, i;
			for (i = 0; i < nCount; i++){
				string strField = fieldStrings[i];
				dt.Columns.Add(strField);
			}
			DataRow dr = dt.NewRow();
			if (dr != null){
				dt.Rows.Add(dr);
				gridview.DataSource = dt;
				gridview.DataBind();

				gridview.Rows[0].Cells.Clear();
				gridview.Rows[0].Cells.Add(new TableCell());
				gridview.Rows[0].Cells[0].ColumnSpan = nCount;
				gridview.Rows[0].Cells[0].Text = EmptyText;
				gridview.Rows[0].Cells[0].Style.Add("text-align", "center");
			}
		}

		public	bool	IsEmptyShowing()
		{
			if (this.Rows.Count == 1 && this.Rows[0].Cells[0].Text == EmptyText)
				return true;
			return false;
		}
	}
}
